package com.geen.tuoke.ui.viewmodel

import android.util.Log
import android.view.View
import androidx.collection.arrayMapOf
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.geen.tuoke.base.BaseViewModel
import com.geen.tuoke.bean.request.LoginRequest
import com.geen.tuoke.network.api.apiService
import com.geen.tuoke.network.utils.CacheUtil
import com.geen.tuoke.utils.ext.request
import com.geen.tuoke.utils.ext.toastShort
import kotlin.math.log

/**
 * @Author LuoJi
 * @Date 2022/1/17-15:31
 * @Desc
 */
class LoginViewModel : BaseViewModel() {

    var account = MutableLiveData<String>().apply {
        value = ""
    }

    var pwd = MutableLiveData<String>().apply {
        value = ""
    }

    var loginSuccess = MutableLiveData<Boolean>()


    fun loginAccount() {
        if (account.value.isNullOrEmpty()) {
            return
        }
        if (pwd.value.isNullOrEmpty()) {
            return
        }
        val loginMap = arrayMapOf<String,String>()
        loginMap["mobile"] = account.value.toString()
        loginMap["password"] = pwd.value.toString()
        request({ apiService.login(loginMap) }, {
             CacheUtil.saveToken(it.token)
             CacheUtil.savePhoneNumber(account.value.toString())
            loginSuccess.value = true
        }, {
            loginSuccess.value = false
            toastShort(it.errorMsg)
        }, true)


    }

    interface LoginHandler {
        fun login(view: View)

        fun clickSwitchPwd(view: View)

        fun clickSwitchMessage(view: View)

        fun clickShowPwd(view:View)
    }
}